'use strict';
let gui_ns = new function() {
	let { panels, graf } = global_ns;
	
	let touch = null;
	let point1 = null;
	
	
	this.createPath = function(touchPos) {
		if(touch.isPress()) {
			for(let i = 0; i < panels.length; i++) {
				let point = null, type = 0;
				
				if(panels[i].outPoint.getDistance(touchPos) < cfg.radius*2) {
					point1 = panels[i];
				} else if(point1 && panels[i].inPoint.getDistance(touchPos) < cfg.radius*2) {
					if(!graf.some(item => item[0] === point1 && item[1] === panels[i])) {
						graf.push([point1, panels[i]]);
							panels[i].node.bind(point1.node);
					};
					point1 = null;
				};
			};
		};
	};
	
	
	this.handleInteractions = function(touches, main) {
		if(!(touch = touches.findTouch() || touch)) return;
		
		let touchPos = main.camera.buf().add(touch);
		this.createPath(touchPos);
	};
	
	
	this.render = function(ctx) {
		ctx.save();
		ctx.beginPath();
		ctx.strokeStyle = '#33ff33';
		for(let i = 0; i < graf.length; i++) {
			let a = graf[i][0].outPoint;
			let b = graf[i][1].inPoint;
			ctx.moveTo(a.x, a.y);
			ctx.lineTo(b.x, b.y);
		};
		ctx.stroke();
		ctx.restore();
		
		for(let i = 0; i < panels.length; i++) panels[i].drawGUI(ctx);
	};
};
